<?php
require_once("Disciplina.php");

class GrupoEletivas{
    public $disciplinas;
    public $totalCreditos;
    
    public function __construct($xmlGrupos){
        $attributes = $xmlGrupos->attributes();
        
        $this->totalCreditos = (string)$attributes['cred'];
        
        $disciplinas = $xmlGrupos->disciplinas;
        foreach($disciplinas->disciplina as $disciplina){
            $this->disciplinas[] = new Disciplina($disciplina);
        }
    }
    
    public function toJson(){
        
        $disciplinasJson = array();
        
        foreach($this->disciplinas as $disciplina){
            $disciplinasJson[] = json_decode($disciplina->toJson());
        }
        
        $json = array(
            'subjects' => $disciplinasJson,
            'credits' => $this->totalCreditos
        );
        //print '<pre>';var_dump($json);print '</pre>';die;
        return json_encode($json);
    }
}